package com.mickey.controller;

import com.mickey.redis.RedisService;
import com.mickey.result.CodeMsg;
import com.mickey.result.Result;
import com.mickey.service.MiaoshaUserService;
import com.mickey.vo.LoginVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Controller
@RequestMapping("/login")
public class LoginController {

    private static Logger log = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    private MiaoshaUserService miaoshaService;

    @Autowired
    private RedisService redisService;

    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/do_login")
    @ResponseBody
    public Result<Boolean> doLogin(@Valid LoginVO loginVO, HttpServletResponse response){
        log.info(loginVO.toString());
        CodeMsg cm = miaoshaService.login(loginVO,response);
        if(cm.getCode() == 0){
            return Result.sucess(true);
        }else{
            return Result.err(cm);
        }
    }




}
